; test of "strcopy" procedure
; author:  R. Detmer   revised:  10/97

.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h            ; header file for input/output
cr      equ     0dh     ; carriage return character
Lf      equ     0ah     ; line feed

.STACK  4096            ; reserve 4096-byte stack

.DATA                   ; reserve storage for data
prompt      BYTE   cr, Lf, "Original string?  ",0
stringIn    BYTE   80 DUP (?)
display     BYTE   cr, Lf, "Your string was...", cr, Lf
stringOut   BYTE   80 DUP (?)

.CODE
_start:     output prompt            ; ask for string
            input  stringIn, 80      ; get source string
            lea    eax, stringOut    ; destination address
            push   eax               ; first parameter
            lea    eax, stringIn     ; source
            push   eax               ; second parameter
            call   strcopy           ; call string copy procedure
            output display           ; print result
            INVOKE  ExitProcess, 0   ; exit with return code 0
PUBLIC _start                        ; make entry point public

strcopy     PROC NEAR32

; Procedure to copy string until null byte in source is copied.
; It is assumed that destination location is long enough for copy.

; Parameters are passed on the stack:
;    (1)  address of destination
;    (2)  address of source

            push   ebp               ;save base pointer
            mov    ebp,esp           ;copy stack pointer

            push   edi               ;save registers and flags 
            push   esi
            pushf

            mov    esi,[ebp+8]       ;initial source address
            mov    edi,[ebp+12]      ;destination
            cld                      ;clear direction flag
whileNoNull:
            cmp    BYTE PTR [esi],0  ;null source byte?
            je     endWhileNoNull    ;stop copying if null
            movsb                    ;copy  one byte
            jmp    whileNoNull       ;go check next byte
endWhileNoNull:
            mov    BYTE PTR [edi],0  ;terminate destination string

            popf                     ;restore flags and registers
            pop    esi
            pop    edi
            pop    ebp
            ret    8                 ;exit procedure, discarding parameters
strcopy     ENDP

            END

